<script setup lang="ts">
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import type { BannerItem, CategoryItem } from '@/types/home'
import CustomNavbar from './components/CustomNavbar.vue'
import XtxSwiper from '@/components/XtxSwiper.vue'
import { getHomeBannerAPI } from '@/services/home'
import CategoryPanel from './components/CategoryPanel.vue'
import { getHomeCategoryAPI } from '@/services/home'
import HotPanel from './components/HotPanel.vue'
import { getHomeHotAPI } from '@/services/home'
import { HotItem } from '@/types/home'

import PageSkeleton from './components/PageSkeleton.vue'
import { useGuessList } from '@/composables'
import XtxGuess from '@/components/XtxGuess.vue'
// import XtxGuess from '@/components/XtxGuess.vue'
// 轮播图数据
const bannerList = ref<BannerItem[]>([])

// 获取轮播图数据
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}
const catagoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  catagoryList.value = res.result
}

const hotList = ref<HotItem[]>([])
const geHometHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}

const isLoading = ref(false)

// 使用 onLoad 生命周期
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), geHometHotData()])

  isLoading.value = false
})

const { guessRef, onScrolltolower } = useGuessList()

const isTriggered = ref(false)
const refresherrefresh = async () => {
  isTriggered.value = true
  // await getHomeBannerData()
  // await getHomeCategoryData()
  // await geHometHotData()
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    geHometHotData(),
    guessRef.value?.getMore(),
  ])
  isTriggered.value = false
}
</script>

<template>
  <CustomNavbar />
  <scroll-view
    :refresher-triggered="isTriggered"
    @refresherrefresh="refresherrefresh"
    refresher-enabled
    @scrolltolower="onScrolltolower"
    class="scroll-view"
    scroll-y
  >
    <PageSkeleton v-if="isLoading"></PageSkeleton>
    <template v-else>
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <CategoryPanel :list="catagoryList"></CategoryPanel>
      <HotPanel :list="hotList"></HotPanel>
      <XtxGuess ref="guessRef"></XtxGuess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
